package com.stox.model;

import java.io.Serializable;
import java.util.Date;

import com.stox.util.Constant;

public class Bar implements Serializable{
	private static final long serialVersionUID = 1L;

	private String scripCode;

	private Date date;

	private double open;

	private double high;

	private double low;

	private double close;

	private double volume;

	private double last;

	private double previousClose;

	private double trades;

	private double turnover;

	public Bar() {
		
	}

	public Bar(String scripCode, String text) {
		String[] tokens = text.split(",");
		setScripCode(scripCode);
		try {
			setDate(Constant.dateFormat.parse(tokens[0]));
			setOpen(Double.parseDouble(tokens[1]));
			setHigh(Double.parseDouble(tokens[2]));
			setLow(Double.parseDouble(tokens[3]));
			setClose(Double.parseDouble(tokens[4]));
			setVolume(Double.parseDouble(tokens[5]));
		} catch (Exception e) {
			System.out.println(scripCode+","+text);
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	@Override
	public String toString() {
		return Constant.dateFormat.format(date) + "," + open + "," + high + ","
				+ low + "," + close + "," + volume+ "," +last+ "," +previousClose+ "," +trades+ "," +turnover;
	}

	public String getScripCode() {
		return scripCode;
	}

	public void setScripCode(String scripCode) {
		this.scripCode = scripCode;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public double getOpen() {
		return open;
	}

	public void setOpen(double open) {
		this.open = open;
	}

	public double getHigh() {
		return high;
	}

	public void setHigh(double high) {
		this.high = high;
	}

	public double getLow() {
		return low;
	}

	public void setLow(double low) {
		this.low = low;
	}

	public double getClose() {
		return close;
	}

	public void setClose(double close) {
		this.close = close;
	}

	public double getVolume() {
		return volume;
	}

	public void setVolume(double volume) {
		this.volume = volume;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((date == null) ? 0 : date.hashCode());
		result = prime * result
				+ ((scripCode == null) ? 0 : scripCode.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Bar other = (Bar) obj;
		if (date == null) {
			if (other.date != null)
				return false;
		} else if (!date.equals(other.date))
			return false;
		if (scripCode == null) {
			if (other.scripCode != null)
				return false;
		} else if (!scripCode.equals(other.scripCode))
			return false;
		return true;
	}

	public double getLast() {
		return last;
	}

	public void setLast(double last) {
		this.last = last;
	}

	public double getPreviousClose() {
		return previousClose;
	}

	public void setPreviousClose(double previousClose) {
		this.previousClose = previousClose;
	}

	public double getTrades() {
		return trades;
	}

	public void setTrades(double trades) {
		this.trades = trades;
	}

	public double getTurnover() {
		return turnover;
	}

	public void setTurnover(double turnover) {
		this.turnover = turnover;
	}

}
